import Vue from 'vue'
import VueRouter from 'vue-router'



Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import (/* webpackChunkName: "login" */ '../Pages/Login/Login.vue')
  },
  {
    path: '/registered',
    name: 'registered',
    component: () => import (/* webpackChunkName: "registered" */ '../Pages/registered/registered.vue')
  },
  {
    path: '/retrievepassword',
    name: 'retrievepassword',
    component: () => import (/* webpackChunkName: "retrievepassword" */ '../Pages/retrievepassword/retrievepassword.vue')
  },
  {
    path: '/retrieveNext',
    name: 'retrieveNext',
    component: () => import (/* webpackChunkName: "retrieveNext" */ '../Pages/retrievepassword/retrieveNext.vue')
  },
  {
    path:"/home",
    name:"home",
    component: () => import (/* webpackChunkName: "home" */  '../Pages/Home/home.vue'),
    meta: { requireLogin: true },
    children: [
      {
        path:'Todo',
        name:"Todo",
        component: ()=> import (/* webpackChunkName: "Todo" */  '../Pages/Home/NavigationPages/Todolist/Todo.vue'),
      },
      {
        path:'calendar',
        name:"calendar",
        component: ()=> import (/* webpackChunkName: "Todo" */  '../Pages/Home/NavigationPages/calendar/calendar.vue'),
      },
      {
        path:'personal',
        name:"personal",
        component: ()=> import (/* webpackChunkName: "Todo" */  '../Pages/Home/NavigationPages/personal/personal.vue'),
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireLogin)) {
    let userInfo = window.localStorage.getItem("token");
    // 如果没有登录
    if (!userInfo) {
      next({
        path: '/login',
      })
    } else {
      // 登录成功放行
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})



export default router
